package iqaUI.web;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import iqaUI.domain.Answer;
import iqaUI.domain.Question;
import iqaUI.http.HttpApacheClient;
import iqaUI.http.HttpConnection;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Book 控制层
 *
 * Created by bysocket on 30/09/2017.
 */
@Controller
@RequestMapping(value = "/iqa")
public class BookController {

    private static Logger LOG = Logger.getLogger(BookController.class);

    private static final String QUESTION_FORM_PATH_NAME = "questionForm";
    private static final String ANSWER_FORM_PATH_NAME = "answerForm";
//    private static final String REDIRECT_TO_BOOK_URL = "redirect:/book";

    @RequestMapping(method = RequestMethod.GET)
    public String getBookList(ModelMap map) {
        Question question = new Question();
        question.setQuestion("汉城是哪个国家的首都");

        map.addAttribute("question",question);
        return QUESTION_FORM_PATH_NAME;
    }

    @RequestMapping(value = "/ask")
    public String createBookForm(@ModelAttribute Question question, ModelMap map) {
        String questionOne = question.getQuestion();
        LOG.info("question is " + questionOne);
//        Map<String,String> questionMap = new HashMap<String,String>();
//        questionMap.put("question", questionOne);

        JSONObject questionJSON = new JSONObject();
        questionJSON.put("question", question.getQuestion());

//        String questionJSON = new String();
//        try
//        {
//            questionJSON = JSON.toJSONString(map);
//        }
//        catch (Exception e)
//        {
//            LOG.error(e.toString());
//            LOG.error("json transfer error");
//        }
        String answers = new String();
        try{
            answers = HttpApacheClient.doPostJson("http://127.0.0.1:36080/qa/qas", questionJSON.toString());
        }
        catch (Exception e)
        {
            LOG.error(e.toString());
        }

        if(null != answers)
        {
            LOG.info(answers.toString());
        }

        JSONObject jsonObject = JSON.parseObject(answers);

        Object answersObject =jsonObject.get("answer");

        JSONArray jsonAnswers = JSONArray.parseArray(answersObject.toString());

        List<Answer> answerList = jsonAnswers.toJavaList(Answer.class);

//        Collections.sort(answerList);

        Answer answerOne = new Answer();

        map.addAttribute("answerList", answerList);
        return ANSWER_FORM_PATH_NAME;
    }

    /**
     * 创建 Book
     * 处理 "/book/create" 的 POST 请求，用来新建 Book 信息
     * 通过 @ModelAttribute 绑定表单实体参数，也通过 @RequestParam 传递参数
     */
//    @RequestMapping(value = "/create", method = RequestMethod.POST)
//    public String postBook(@ModelAttribute Book book) {
//        bookService.insertByBook(book);
//        return REDIRECT_TO_BOOK_URL;
//    }

    /**
     * 获取更新 Book 表单
     *    处理 "/book/update/{id}" 的 GET 请求，通过 URL 中的 id 值获取 Book 信息
     *    URL 中的 id ，通过 @PathVariable 绑定参数
     */
//    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
//    public String getUser(@PathVariable Long id, ModelMap map) {
//        map.addAttribute("book", bookService.findById(id));
//        map.addAttribute("action", "update");
//        return BOOK_FORM_PATH_NAME;
//    }

    /**
     * 更新 Book
     * 处理 "/update" 的 PUT 请求，用来更新 Book 信息
     */
//    @RequestMapping(value = "/update", method = RequestMethod.POST)
//    public String putBook(@ModelAttribute Book book) {
//        bookService.update(book);
//        return REDIRECT_TO_BOOK_URL;
//    }

    /**
     * 删除 Book
     * 处理 "/book/{id}" 的 GET 请求，用来删除 Book 信息
     */
//    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
//    public String deleteBook(@PathVariable Long id) {
//        bookService.delete(id);
//        return REDIRECT_TO_BOOK_URL;
//    }

}
